import time
from pyecharts.charts import Line
import akshare as ak
import pyecharts.options as opts
from pyecharts.globals import ThemeType

# 指数走势对比
startDate = '20050101'
endDate = time.strftime('%Y%m%d', time.localtime(time.time()))
indexList = ['399989', '000991','399997', '980017']
nameList = ['中证医疗', '全指医药', '中证白酒', '半导体']
xData = []
yDataList = []
for index in indexList:
    df = ak.index_zh_a_hist(symbol=index, start_date=startDate, end_date=endDate, period="daily")
    xData = df['日期'].tolist()
    yDataList.append(df['收盘'])


# 折线图
chart = Line(
    # 设置主题、画布宽度、高度等信息
    init_opts=opts.InitOpts(theme=ThemeType.DARK, width="1400px", height="600px"),
)

# 全局设置
chart.set_global_opts(
    title_opts=opts.TitleOpts(title="", ),
    # 工具箱配置
    toolbox_opts=opts.ToolboxOpts(is_show=True),
    # 区域缩放配置
    datazoom_opts=[opts.DataZoomOpts(is_show=True, range_start=0, range_end=100)],
)

chart.add_xaxis(xaxis_data=xData)  # 添加x轴数据
for i, d in enumerate(yDataList):
    chart.add_yaxis(
        series_name=nameList[i],
        y_axis=d,  # y轴数据
        is_connect_nones=True,  # 连接空数据
        markpoint_opts=opts.MarkPointOpts(
            data=[opts.MarkPointItem(type_="max"), opts.MarkPointItem(type_="min")],
        ),
    )

chart.render(f"指数走势对比-{endDate}.html")

